package com.fanketly.musicdiary.utls

import com.fanketly.musicdiary.data.moodMap


object MoodUtils {
    //42-47
//    fun guessMood(content: String): String {
//        var mood = ""
//        var num = 0
//        moodMap.keys.forEach {
//            var localContent = content
//            var localnum = 0
//            while (localContent.indexOf(it) != -1) {
//                localnum++
//                localContent = localContent.substring(localContent.indexOf(it) + it.length)
//            }
//            if (localnum > num) {
//                mood = it
//                num = localnum
//            }
//        }
//        return mood
//    }

    //41-46 fastest
    fun guessMood(content: String): String {
        var mood = ""
        var num = 0
        val stringBuilder = StringBuilder()
        moodMap.keys.forEach {
            stringBuilder.append(content)
            val localNum =
                (content.length - stringBuilder.replace(Regex(it), "").length) / it.length
            //the larger the number of occurrences
            if (localNum > num) {
                mood = it
                num = localNum
            }
            stringBuilder.clear()
        }
        return mood
    }

//    fun moodText(content: String, mood: String) =
//        buildAnnotatedString {
////            moodMap.keys.forEach {
//            if (mood.isEmpty()) {
//                append(content)
//                return@buildAnnotatedString
//            }
//            val moodLength = mood.length
//            val moodEnum = moodMap[mood]
//            var localContent = content
//            var indexOf = localContent.indexOf(mood)
//            while (indexOf != -1) {
//                append(localContent.substring(0, indexOf))
//                withStyle(
//                    style = SpanStyle(
//                        color = moodEnum!!.color,
//                        fontWeight = FontWeight.Bold
//                    )
//                ) {
//                    append(localContent.substring(indexOf, indexOf + moodLength))
//                }
//                localContent = localContent.substring(indexOf + moodLength)
//                indexOf = localContent.indexOf(mood)
//            }
//            append(localContent)
////            }
//        }

    //44-48 the easiest
//    fun guessMood(content: String): String {
//        var mood = ""
//        var num = 0
//        moodMap.keys.forEach {
//            val localNum = (content.length - content.replace(it, "").length) / it.length
//            //the larger the number of occurrences
//            if (localNum > num) {
//                mood = it
//                num = localNum
//            }
//        }
//        return mood
//    }
}
